import { dayjs } from 'element-plus'

//传入当前时间 返回 yyyy-mm-dd格式 日期 num？
export function formatTime(time,num){
    let time1 = null;
    if(time.toString().length <= 10) time1 = new Date(time*1000);
    else time1 = new Date(time);
                                                                                //formatVideoSpeedTime(time1.getDate() - num) num不知其作用
    return time1.getFullYear()+'-'+formatVideoSpeedTime(time1.getMonth()+1)+'-'+formatVideoSpeedTime(time1.getDate())+' '+formatVideoSpeedTime(time1.getHours())+':'+formatVideoSpeedTime(time1.getMinutes())
}
//传入时间返回 xx/0x 格式
export function formatVideoSpeedTime(time){
    let time1 = time;
    let time2 = null;
    if(time < 10) time1 = "0" + time
    if(time > 60){
        time2 = Math.floor(time / 60)
        if( Math.floor(time / 60) < 10 ) time2 = "0"+ Math.floor(time / 60)
        time1 = Math.floor(time1 % 60) 
        if( Math.floor(time % 60) < 10 ) time1 = "0"+ Math.floor(time % 60)
        return time2+":"+time1
    }
    return time1
    
}
//两个值 返回 看了多久/时长 
export function videoSpeedTime(val1,val2){
    if(val1 == -1) val1 = val2
    return formatVideoSpeedTime(Math.floor(val1 / 60))+":"+formatVideoSpeedTime(val1 % 60)+"/"+formatVideoSpeedTime(Math.floor(val2 / 60))+":"+formatVideoSpeedTime(val2 % 60)
}
//计算返回视频的时长
export function voideoAllTime(val){
    return formatVideoSpeedTime(Math.floor(val / 60)) + ":" + formatVideoSpeedTime(val % 60)
}
//传入数返回 xx.xx万/亿 
export function formatNum(val){
    let data = 0
    if(val > 10000 && val < 100000000){
        let num = (Math.floor((val / 10000) * 100) / 100).toFixed(1) - (parseInt(Math.floor((val / 10000) * 100) / 100))
        if(num == 0) data = parseInt(Math.floor((val / 10000) * 100) / 100) + "万"
        else data = Number((Math.floor((val / 10000) * 100) / 100).toFixed(1)) + "万"
    }else if(val < 10000){
        data = val
    }else{
        let num = (Math.floor((val / 100000000) * 100) / 100).toFixed(1)-(parseInt(Math.floor((val / 100000000) * 100) / 100))
        if(num == 0) data = parseInt(Math.floor((val / 100000000) * 100) / 100) + "亿"
        else data = Number((Math.floor((val /100000000) * 100) / 100).toFixed(1)) + "亿"
    }

    return data
}
//视频后方卡片为国创和番剧是计算返回喜欢人数  根据情况删除/系列追番/追番/
export function delNumString(val){
    const regex = /系列追番/
    let size = 0
    if (val.toString().search(regex) === -1) {
         size= val.length - 2
    } else {
        size = val.length - 4
    }
    return val.toString().slice(0,size)
}
//time：时间戳  返回传入时间戳的日期+时间：今天/昨天/更早：xx-xx（日期）  xx：xx（时间）
export function NowBetwDate(time){
    let toDayTime = dayjs().startOf('d').unix()
    let yeDayTime = dayjs().startOf('d').unix()- 86400
    let timer = null;
    let month = (dayjs.unix(time).$M + 1 ) < 10? '0' + (dayjs.unix(time).$M + 1 ) : (dayjs.unix(time).$M + 1 )
    let day = dayjs.unix(time).$D < 10? '0' + dayjs.unix(time).$D : dayjs.unix(time).$D
    let hours = dayjs.unix(time).$H < 10? '0' + dayjs.unix(time).$H : dayjs.unix(time).$H
    let min = dayjs.unix(time).$m < 10?'0'+dayjs.unix(time).$m : dayjs.unix(time).$m
    if (time >= toDayTime) {
        timer = '今天' + '\xa0\xa0' + hours + ':' + min
    } else if ( yeDayTime <= time &&  time < toDayTime) {
        timer = '昨天' + '\xa0\xa0' + hours + ':' + min
    } else {
        timer = month + '-' + day + '\xa0\xa0' + hours + ':' + min
    }
    return timer
}
//time：时间戳  返回传入时间戳的日期+时间：今天/昨天/更早：xxxx-xx-xx（日期）  xx：xx（时间）
export function NowBetwYMDDate(time){
    let toDayTime = dayjs().startOf('d').unix()
    let yeDayTime = dayjs().startOf('d').unix()- 86400
    let timer = null;
    let year = dayjs.unix(time).$y
    let month = (dayjs.unix(time).$M + 1 ) < 10? '0' + (dayjs.unix(time).$M + 1 ) : (dayjs.unix(time).$M + 1 )
    let day = dayjs.unix(time).$D < 10? '0' + dayjs.unix(time).$D : dayjs.unix(time).$D
    let hours = dayjs.unix(time).$H < 10? '0' + dayjs.unix(time).$H : dayjs.unix(time).$H
    let min = dayjs.unix(time).$m < 10?'0'+dayjs.unix(time).$m : dayjs.unix(time).$m
    if (time >= toDayTime) {
        timer = '今天' + '\xa0\xa0' + hours + ':' + min
    } else if ( yeDayTime <= time &&  time < toDayTime) {
        timer = '昨天' + '\xa0\xa0' + hours + ':' + min
    } else {
        timer =  year + '-' + month + '-' + day + '\xa0\xa0' + hours + ':' + min
    }
    return timer
}
/**
 * 
 * @param {时间戳} time 
 * @returns yyyy-mm-dd
 */
export function formatYMD(time){
    let year = dayjs.unix(time).$y
    let month = (dayjs.unix(time).$M + 1 ) < 10? '0' + (dayjs.unix(time).$M + 1 ) : (dayjs.unix(time).$M + 1 )
    let day = dayjs.unix(time).$D < 10? '0' + dayjs.unix(time).$D : dayjs.unix(time).$D
    return year + '-' + month + '-' + day 
}
//time: 时间戳 返回周几+xx：xx（时间）
export function weekData(time){
    let week = dayjs.unix(time)
    let weekData = null
    if (week.$W !== 0) {
       weekData = '周' + new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec").format(week.$W)
    } else {
        weekData = '周日'
    }
    let hours = week.$H < 10? '0' + week.$H : week.$H
    let min = week.$m < 9?'0' + week.$m : week.$m

    return weekData + '\xa0\xa0' + hours + ':' + min
}
/**
 * 返回 x-x 日期格式
 * @param {时间戳} time 
 */
export function formatMD(time){
    return dayjs.unix(time).$M + 1 + '-' + dayjs.unix(time).$D
}
/**
 * 判断是否时当前年份 是则不用返回年 不是则需要返回年份返回 x-x-x 日期格式
 * @param {时间戳} time 
 */
export function formatIsYMD(time){
    return (dayjs.unix(time).$y == dayjs().format('YYYY')? '' : dayjs.unix(time).$y + '-') + (dayjs.unix(time).$M + 1) + '-' + dayjs.unix(time).$D
}
/**
 * 
 * @param {时间戳} time 
 * 传入时间戳 查看周几
 */
export function formatWeek(time){
    let week = dayjs.unix(time)
    let weekData = null
    if (week.$W !== 0) {
       weekData = '周' + new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec").format(week.$W)
    } else {
        weekData = '周日'
    }
    return weekData
}
/**
 * 
 * @param {视频时间字符串} time 
 * 传入时间字符串 根据‘：’分割再处理为时间格式返回
 */
export function formatStrVideoTime(time) {
    let arr = time.split(':')
    if (Number(arr[0]) > 60) {
        let time = Number(arr[0])
        let time2 = Number(arr[1])
        return (Math.floor(time / 60) < 10? '0' + Math.floor(time / 60) : Math.floor(time / 60)) + ':' + (time % 60 < 10? '0' + time % 60 : time % 60) + ':' + (time2 < 10? '0' + time2 : time2)
    } else {
        let time = Number(arr[0])
        let time2 = Number(arr[1])
        return (time < 10? '0' + time : time) + ':' + (time2 < 10? '0' + time2 : time2)
    }
}